﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace NPCScriptEditor
{
    public partial class MDIP : Form
    {
        private int childFormNumber = 0;
        public Form1 formo;
        public MDIP(Form1 form,string[] codes)
        {
            formo = form;
            InitializeComponent();
            if (codes.Length !=0) aLoad(codes);
            Debug.WriteLine(codes.Length.ToString());
        }

        private  void aLoad(string[] codes)
        {
            for (int i = 0; i < codes.Length; i++)

            {

                Windows.wineditor childForm = new Windows.wineditor("");
                childForm.MdiParent = this;
                childForm.fastColoredTextBox1.Text = codes[i].ToString();
                childForm.Text = "页 " + childFormNumber++;
                childForm.Show();

            }
            //foreach (string item in codes)
            //{
            //    Debug.WriteLine(item.ToString());
            //}
            //throw new NotImplementedException();
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            Windows.wineditor childForm = new Windows.wineditor("");
            childForm.MdiParent = this;
            childForm.Text = "页 " + childFormNumber++;
            childForm.Show();
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            openFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = openFileDialog.FileName;
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var qwq = (Windows.wineditor)this.ActiveMdiChild;
            qwq.fastColoredTextBox1.Copy();
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var qwq = (Windows.wineditor)this.ActiveMdiChild;
            qwq.fastColoredTextBox1.Paste();
            //qwq.fastColoredTextBox1.Selection


        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("软件版本:0.02#娃娃版#预览#概念版pre4");
        }

        private void MDIP_FormClosed(object sender, FormClosedEventArgs e)
        {
            formo.Visible = true;
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            save();
        }

        private void save()
        {
            List<string> qwq=new List<string>();
            foreach (Windows.wineditor item in this.MdiChildren)
            {
                qwq.Add(item.fastColoredTextBox1.Text);

            }
            if (Program.path == "")
            {

                SaveFileDialog o = new SaveFileDialog();
                o.Title = "保存为";
                o.Filter = "工程(*.ns)|*.ns";
                if (o.ShowDialog() == DialogResult.OK) Program.path = o.FileName;
                
                
            }

            Classes.SaveLoad.Save(qwq,Program.path);
            //throw new NotImplementedException();
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            save();
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {

        }
    }
}
